package com.tange.module.device.info.pwd;

import android.text.TextUtils;

import com.tange.core.backend.service.response.RxResponse;
import com.tange.module.device.info.DeviceInfoServiceManager;
import com.tg.appcommon.android.TGLog;
import com.tg.data.bean.DeviceItem;
import com.tange.core.backend.service.http.ClientObserver;
import com.tg.network.socket.http.TGHttp;

import java.util.HashMap;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 设备密码查询
 */
public class DevicePasswordQueryServiceImpl implements DevicePasswordQueryService {

    private static final String TAG = DeviceInfoServiceManager.TAG + "Password";

    @Override
    public Observable<RxResponse<DeviceItem>> query(String deviceId) {

        return Observable.create(emitter -> {

            if (emitter == null || emitter.isDisposed()) {
                return;
            }

            if (TextUtils.isEmpty(deviceId)) {
                RxResponse rxResult = new RxResponse();
                rxResult.isSuccess = false;
                rxResult.errorMsg = "deviceId empty";
                emitter.onNext(rxResult);
                return;
            }

            final HashMap map = new HashMap();
            map.put("device_id", deviceId);

            TGLog.i(TAG, "[query] params = " + map);

            TGHttp.getInstance().getDevicePassword(map)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new ClientObserver<DeviceItem>() {
                        @Override
                        protected void onSuccess(final DeviceItem item) {
                            TGLog.i(TAG, "[query][onSuccess] item = " + item);
                            if (emitter == null || emitter.isDisposed()) {
                                return;
                            }
                            RxResponse rxResult = new RxResponse();
                            rxResult.isSuccess = true;
                            rxResult.content = item;
                            emitter.onNext(rxResult);
                        }

                        @Override
                        protected void onResponseError(int errorCode, String errorInfo) {
                            String msg = "errorCode = " + errorCode + " , errorInfo = " + errorInfo;
                            TGLog.i(TAG, "[query][onResponseError] " + msg);
                            if (emitter == null || emitter.isDisposed()) {
                                return;
                            }
                            RxResponse rxResult = new RxResponse();
                            rxResult.isSuccess = false;
                            rxResult.errorMsg = msg;
                            emitter.onNext(rxResult);
                        }

                        @Override
                        protected void onOtherError(String error) {
                            TGLog.i(TAG, "[query][onOtherError] " + error);
                            if (emitter == null || emitter.isDisposed()) {
                                return;
                            }
                            RxResponse rxResult = new RxResponse();
                            rxResult.isSuccess = false;
                            rxResult.errorMsg = error;
                            emitter.onNext(rxResult);
                        }
                    });
        });
    }
}
